package com.tsd.system.controller;

import com.tsd.core.aspect.MethodDurationAspect;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HttpResult;
import com.tsd.core.vo.MethodDuration;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * 描述：系统性能查看 控制层
 *
 * @author Hillpool
 * @date 2023/09/10 12:20:51
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
public class SysPerformanceController extends SystemBaseController {
    @ApiOperation(value = "根据id获取SysRole")
    @ResponseBody
    @RequestMapping(value = "/getControllerMethodDurations.action", method = RequestMethod.POST)
    public HttpResult<Object> getControllerMethodDurations() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, MethodDuration> map = MethodDurationAspect.getDurationMap();
            Set<String> keys = map.keySet();
            List<MethodDuration> list = new ArrayList<>();
            for (String key : keys) {
                list.add(map.get(key));
            }
            Collections.sort(list, new Comparator<MethodDuration>() {
                @Override
                public int compare(MethodDuration o1, MethodDuration o2) {
                    return (int) (o2.getAvg() - o1.getAvg());
                }
            });
            hr.setData(list);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }
}
